package com.feinno.module.security.domain;

import java.util.Date;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;
import org.hibernate.annotations.OrderBy;
import org.hibernate.annotations.Parameter;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.feinno.framework.common.domain.AbstractEntity;
import com.feinno.module.security.SecurityConstants;

@Entity
@Table(name = "SYS_RESOURCE")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@JsonIgnoreProperties(value = { "roles", "handler" }, ignoreUnknown = true)
public class Resource extends AbstractEntity {
	/** uid */
	private static final long serialVersionUID = -8335507697312033450L;

	/** 名称 */
	private String name;

	/** 类型 */
	private String resType;

	/** 资源串 */
	private String resString;

	private String icon;

	/** 描述 */
	private String descn;

	/** 排序时间(默认倒序) */
	private Date orderTime = new Date();

	/** 是否显示(0:显示,1:隐藏) */
	private int showState = SecurityConstants.SHOW_STATE_YES;

	/** 显示方式(用于easyUI或EXTJS,1:AJAXLOAD,2:IFRAME) */
	private int showMode = SecurityConstants.SHOW_MODE_AJAXLOAD;

	/** 包含角色 */
	private Set<Role> roles;

	/** 归属菜单 */
	private Menu menu;

	@Id
	@GeneratedValue(generator = "sequence")
	@GenericGenerator(name = "sequence", strategy = "sequence", parameters = { @Parameter(name = "sequence", value = "SEQ_SYS_RESOURCE") })
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name = "NAME")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "RESTYPE")
	public String getResType() {
		return resType;
	}

	public void setResType(String resType) {
		this.resType = resType;
	}

	@Column(name = "RES_STRING")
	public String getResString() {
		return resString;
	}

	public void setResString(String resString) {
		this.resString = resString;
	}

	@Column(name = "DESCN")
	public String getDescn() {
		return descn;
	}

	public void setDescn(String descn) {
		this.descn = descn;
	}

	@ManyToMany(mappedBy = "rescs", targetEntity = com.feinno.module.security.domain.Role.class)
	@Cascade(value = { CascadeType.SAVE_UPDATE })
	@OrderBy(clause = "role_id")
	@LazyCollection(value = LazyCollectionOption.EXTRA)
	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	@ManyToOne
	@JoinColumn(name = "MENUID")
	@LazyToOne(value = LazyToOneOption.PROXY)
	public Menu getMenu() {
		return menu;
	}

	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public Date getOrderTime() {
		return orderTime;
	}

	public void setOrderTime(Date orderTime) {
		this.orderTime = orderTime;
	}

	public int getShowState() {
		return showState;
	}

	public void setShowState(int showState) {
		this.showState = showState;
	}

	public int getShowMode() {
		return showMode;
	}

	public void setShowMode(int showMode) {
		this.showMode = showMode;
	}

	@Override
	public String toString() {
		return String.format(
				"Resource: {name:%s, resType:%s, resString:%s, descn:%s}",
				name, resType, resString, descn);
	}

}
